package com.miraksoft.statewatcher.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.text.style.ForegroundColorSpan;

public class WatchedObjectRuntime  implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3443684414413947809L;
	
	private WatchedObjectDef watchedObjectDef = null;
	private List<WatchedObjectStateRuntime> stateRuntimes = new ArrayList<WatchedObjectStateRuntime>();

	public WatchedObjectRuntime( WatchedObjectDef watchedObjectDef ){
		this.watchedObjectDef = watchedObjectDef;
	}
	
	public WatchedObjectDef getWatchedObjectDef() {
		return watchedObjectDef;
	}

	public List<WatchedObjectStateRuntime> getStateRuntimes() {
		return stateRuntimes;
	}

	public void setStateRuntimes(List<WatchedObjectStateRuntime> stateRuntimes) {
		this.stateRuntimes = stateRuntimes;
	}
	
	public WatchedObjectStateRuntime addWatchedObjectStateRuntime( WatchedObjectStateDef watchedObjectStateDef){
		
		WatchedObjectStateRuntime stateRuntime = new WatchedObjectStateRuntime(watchedObjectStateDef);
		stateRuntimes.add(stateRuntime);
		stateRuntime.setParentWatchedObjectRuntime(this);
		return stateRuntime;
		
	}
	
	public WatchedObjectStateRuntime getRuntimeStateByButtonId( Integer btnId ){

		for( WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			
			if( btnId.equals( stateRuntime.getButton().getId()) ){
				return stateRuntime;
			}
		}
		
		return null;
	}

	public WatchedObjectStateRuntime getRunningStateRuntime() {
		
		for ( WatchedObjectStateRuntime stateRuntime : getStateRuntimes()) {
			
			if( stateRuntime.isRunning()){
				return stateRuntime ;
			}
			
		}
		
		return null;
	}
	
	public String toString(){
		
		StringBuffer out = new StringBuffer();
		out.append("watchedObjectDef: " + watchedObjectDef.getName() + "\n" + "stateRuntimes: ");
		for (WatchedObjectStateRuntime stateRuntime : stateRuntimes) {
			out.append(stateRuntime.getWatchedObjectStateDef().getName() + ",");
		}
		
		return out.toString();

	}
}
